home *** CD-ROM | disk | FTP | other *** search
- /* ----------------------------------------------------------------------
-
- PICTButton CDEF
- version 1.3.1
-
- Written by: Paul Celestin
-
- Copyright © 1993-1996 Celestin Company, Inc.
-
- This CDEF displays a picture whose resource ID is derived from
- the min, max, and value fields of the CNTL. For a multiple-state
- button, set min to ID of the the first PICT and max to the ID of
- the last PICT. Also set value to the ID of the first PICT.
-
- 930822 - 1.0.0 initial release
- 930829 - 1.0.1 added variations 1 and 2
- 930915 - 1.0.2 fixed the new variations
- 930922 - 1.0.3 fixed a problem with black and white PICTs showing
- up, or so I thought
- 931012 - 1.0.4 changed color of text in variation 2 to blue
- 931012 - 1.0.5 really fixed problem with black and white PICTs
- showing up properly
- 940319 - 1.0.6 removed unnecessary erasing code
- 940703 - 1.0.7 now works over multiple monitors of different bit
- depths
- 950804 - 1.1.0 finally converted this thing over to CodeWarrior!
- 950809 - 1.2.0 fixed bug in hasColor that caused computer to crash
- if button was partially or completely offscreen
- 951215 - 1.3.0 updated to CW7
- 960704 - 1.3.1 updated to CW9
-
- ---------------------------------------------------------------------- */
-
- #define kPlain 0
- #define kInverted 1
- #define kInvertedPICT 100
- #define kOffset 200
- #define kIsColorPort 0xC000 // If these bits are set, its a CGrafPort.
-
-
- /* ----------------------------------------------------------------------
- prototypes
- ---------------------------------------------------------------------- */
- pascal long main(short variation, ControlHandle theControl, short message, long param);
- int hasColor(Rect);
- void drawIt(ControlHandle control,short variation);
- long testIt(ControlHandle control, Point myPoint);
-
-
- /* ----------------------------------------------------------------------
- main
- ---------------------------------------------------------------------- */
- pascal long main(short variation, ControlHandle theControl, short message, long param)
- {
- long returnValue = 0L;
- char state = HGetState((Handle)theControl);
- Str255 copyright = "\pCopyright © 1993-1996 Celestin Company, Inc.";
-
- switch(message)
- {
- case drawCntl:
- drawIt(theControl,variation);
- case testCntl:
- returnValue = testIt(theControl, *(Point *) ¶m);
- case calcCRgns:
- break;
- case initCntl:
- break;
- case dispCntl:
- break;
- case posCntl:
- break;
- case thumbCntl:
- break;
- case dragCntl:
- break;
- case autoTrack:
- break;
- case calcCntlRgn:
- break;
- case calcThumbRgn:
- break;
- default:
- break;
- }
-
- HSetState((Handle)theControl,state);
-
- return(returnValue); /* tell them what happened */
- }
-
-
- /* ----------------------------------------------------------------------
- hasColor
- ---------------------------------------------------------------------- */
- int hasColor(Rect r)
-
- {
- SysEnvRec myComputer;
- GDHandle curDev;
- PixMapHandle myPixMap;
-
- SysEnvirons(2,&myComputer);
- if (myComputer.hasColorQD)
- {
- LocalToGlobal((Point*) &r);
- LocalToGlobal(1 + (Point*) &r);
- curDev = GetMaxDevice(&r);
- if (curDev != nil)
- {
- myPixMap = (**curDev).gdPMap;
- if ((**myPixMap).pixelSize > 1)
- return(1);
- else
- return(0);
- }
- }
- return(0);
- }
-
-
- /* ----------------------------------------------------------------------
- drawIt - here is where we actually draw the control
- ---------------------------------------------------------------------- */
- void drawIt(ControlHandle control, short variation)
-
- {
- short invert = 0, useBW = 0;
- int savedFont, savedSize, savedMode;
- Rect myRect;
- GrafPtr thePort;
- PicHandle myPicture;
- PenState oldPenState;
- Str255 myTitle;
- Pattern myGray;
-
- GetPort(&thePort); /* save off the current port */
-
- if (!(*control)->contrlVis) /* if not visible, do nothing */
- return;
-
- myRect = (*control)->contrlRect;
-
- if (!hasColor(myRect)) /* use the B&W PICTs */
- useBW = kOffset;
-
- if ((*control)->contrlHilite == inButton)
- {
- invert = kInvertedPICT; /* invert while tracking */
- SetControlReference(control, kInverted);
- }
- else
- {
- if ( (GetControlReference(control) == kInverted) && (!Button()) )
- {
- SetControlReference(control, kPlain);
- if (GetControlValue(control) == GetControlMaximum(control))
- SetControlValue(control,GetControlMinimum(control));
- else
- SetControlValue(control,GetControlValue(control) + 1);
- }
- }
-
- myPicture = (PicHandle)GetResource('PICT', (GetControlValue(control) + invert + useBW));
-
- if ( myPicture == 0L ) /* could not find the PICT */
- return;
-
- LoadResource((Handle)myPicture);
- HNoPurge((Handle)myPicture);
-
- DrawPicture(myPicture, &myRect); /* draw the picture */
-
- switch (variation)
- {
- case 1: /* display title of control in geneva 9 */
- {
- savedFont = thePort->txFont; /* save off current values */
- savedSize = thePort->txSize;
- savedMode = thePort->txMode;
-
- ForeColor(blueColor);
- TextFont(geneva);
- TextSize(9);
- TextMode(srcOr);
-
- BlockMove(((*control)->contrlTitle),myTitle,((*control)->contrlTitle)[0] + 1);
-
- MoveTo((myRect.right+myRect.left) / 2 - StringWidth(myTitle) / 2, myRect.bottom - 6);
- DrawString(myTitle);
-
- TextFont(savedFont); /* restore saved values */
- TextSize(savedSize);
- TextMode(savedMode);
- ForeColor(blackColor);
-
- break;
- }
- case 2: /* simple 2-pixel wide black border */
- {
- PenSize(2,2);
- InsetRect(&myRect,-3,-3);
- FrameRect(&myRect);
- InsetRect(&myRect,3,3);
- PenSize(1,1);
-
- break;
- }
- }
-
- if ((*control)->contrlHilite == 255) /* gray out the picture */
- {
- GetPenState(&oldPenState);
- PenNormal();
- GetIndPattern(&myGray,0,4);
- PenPat(&myGray);
- PenMode(patBic);
- PaintRect(&myRect);
- SetPenState(&oldPenState);
- }
-
- HPurge((Handle)myPicture);
-
- return; /* we are done drawing */
- }
-
-
- /* ----------------------------------------------------------------------
- testIt - test for mouse hits within control
- ---------------------------------------------------------------------- */
- long testIt(ControlHandle control, Point myPoint)
-
- {
- Rect myRect;
-
- myRect = (*control)->contrlRect;
-
- if (((*control)->contrlVis != 0) && ((*control)->contrlHilite != 255) &&
- (PtInRect(myPoint,&myRect)))
- return(inButton);
- else
- return(0);
- }
-